package com.cortex.papaya.view;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.ClipData;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.View.DragShadowBuilder;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.FrameLayout;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cortex.papaya.adapter.CategoryAdapter;
import com.cortex.papaya.adapter.DocumentAdapter;
import com.cortex.papaya.adapter.DocumentAdapter.OnClickDocumentListener;
import com.cortex.papaya.listener.SlidViewListener;
import com.cortex.papaya.listener.SlidViewListener.SlidDirection;
import com.cortex.papaya.search.Categories;
import com.cortex.papaya.search.Category;
import com.cortex.papaya.search.Link;
import com.cortex.papaya.search.Link.OnDataChangedListener;
import com.cortex.papaya.utilities.MyLog;
import com.cortex.papaya.view.LinkView.OnClickDocumentView;
import com.cortex.papayasearch.R;

public class ResultsView extends LinearLayout {

	private GridView gridDocuments;
	private OnSelectedDocumetListener onSelectedDocument;
	private TextView tvCategoryTitle,tvCountResultsTitle;
	private Categories categories;
	private ListView categoriesList;
	private View lineSeparation;

	public ResultsView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public ResultsView(Context context) {
		super(context);
	}
	@Override
	protected void onFinishInflate() {
		// TODO Auto-generated method stub
		super.onFinishInflate();
		init();
	}
	protected void init(){
		gridDocuments = (GridView)findViewById(R.id.gridDocuments);
		tvCategoryTitle = (TextView)findViewById(R.id.tvCategoryTitle);
		tvCountResultsTitle = (TextView)findViewById(R.id.tvCountResultsTitle);
		categoriesList = (ListView)findViewById(R.id.categoriesList);
		lineSeparation = findViewById(R.id.lineSeparation);
		initListeners();
	}
	private void initListeners(){
		categoriesList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int position,
					long id) {
				Category category = ((CategoryView)view).getCategory();
				categories.setSelectedCategory(category);
				showCategory(category);
			}
		});
		categoriesList.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> adpterView, View v,
					int position, long id) {
				// create drop and drag on view
				ClipData data = ClipData.newPlainText("", "");
				DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
				v.startDrag(data, shadowBuilder, v, 0);
				// invisible to view
				v.setVisibility(View.INVISIBLE);
				return true;
			}
		});
		lineSeparation.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) categoriesList.getLayoutParams();
				switch(event.getAction()){
				case MotionEvent.ACTION_DOWN:
					params.weight = 0;
					params.width = categoriesList.getWidth();
					categoriesList.setLayoutParams(params);	
					return true;
				case MotionEvent.ACTION_MOVE:
					params.width += (int) event.getX();
					categoriesList.setLayoutParams(params);	
					break;
				case MotionEvent.ACTION_UP:
				case MotionEvent.ACTION_CANCEL:
					
					
					break;
				}
				return false;
			}
		});
	}
	private void showCategory(Category category){
		tvCategoryTitle.setText(category.getName());
		tvCountResultsTitle.setText("(" + category.getCountLinks() + "results)");
		if(gridDocuments.getAdapter() instanceof DocumentAdapter){
			DocumentAdapter adapter = (DocumentAdapter) gridDocuments.getAdapter();
			adapter.setDocuments(category.getLinks());
		}
		gridDocuments.invalidate();
	}
	public void show(Categories categories){
		this.categories = categories;
		categoriesList.setAdapter(new CategoryAdapter(getContext(),categories.getCategories()));
		
		DocumentAdapter adapter = new DocumentAdapter(getContext(), categories.getAllLinks());
		adapter.setOnClickDocumentListener(new OnClickDocumentListener() {
			
			@Override
			public void documentClicked(LinkView documentView) {
				if(onSelectedDocument != null){
					onSelectedDocument.selectedDocument(documentView.getDocument());
				}
			}
		});
		gridDocuments.setAdapter(adapter);
		showCategory(categories.getSelectedCategory());
	}
	public void setOnSelectedDocumentListener(OnSelectedDocumetListener selectedDocument){
		this.onSelectedDocument = selectedDocument;
	}

	public interface OnSelectedDocumetListener{
		void selectedDocument(Link document);
	}

	public boolean isClear() {
		return gridDocuments.getChildCount()>0;
	}
}
